package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.google.common.collect.Lists;
import com.sc.nft.dao.EcologyHatchRecordsRewardsDao;
import com.sc.nft.dao.UserPredictEcologyHatchRewardsRecordsDao;
import com.sc.nft.dto.*;
import com.sc.nft.entity.*;
import com.sc.nft.enums.EcologyRewardsType;
import com.sc.nft.enums.MallRefundStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.UserPredictEcologyHatchRewardsRecordsService;
import com.sc.nft.util.BigDecimalUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生态-孵化-用户待结算奖励记录表 服务实现类
 * </p>
 *
 * @author liGen
 * @since 2024-08-07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserPredictEcologyHatchRewardsRecordsServiceImpl implements UserPredictEcologyHatchRewardsRecordsService {

    private final UserPredictEcologyHatchRewardsRecordsDao userPredictEcologyHatchRewardsRecordsDao;
    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;
    private final EcologyHatchRecordsRewardsDao ecologyHatchRecordsRewardsDao;
    private final EcologyHatchConfDao ecologyHatchConfDao;
    private final EcologyHatchRewardsConfDao ecologyHatchRewardsConfDao;
    private final EcologyCastExpendConfDao ecologyCastExpendConfDao;
    private final EcologyCastConfDao ecologyCastConfDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long userEcologyHatchRecordId) {
        List<UserPredictEcologyHatchRewardsRecords> userPredictEcologyHatchRewardsRecordsList = new ArrayList<>();
        // 获取熔炼记录
        UserEcologyHatchRecords userEcologyHatchRecords = userEcologyHatchRecordsDao.getById(userEcologyHatchRecordId);
        Boolean hasList = userPredictEcologyHatchRewardsRecordsDao.hasList(userEcologyHatchRecords.getUserId(), userEcologyHatchRecords.getId());
        if (hasList){
           return;
        }
        Assert.notNull(userEcologyHatchRecords, () -> new GlobalRunTimeException("熔炼记录不存在"));
        // 获取快照奖励
        List<EcologyHatchRecordsRewards> ecologyHatchRecordsRewards = ecologyHatchRecordsRewardsDao.getListByEcologyHatchRecordsId(userEcologyHatchRecordId);
        Assert.isFalse(CollectionUtils.isEmpty(ecologyHatchRecordsRewards), () -> new GlobalRunTimeException("快照奖励不存在"));
        // 获取熔炼配置
        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(userEcologyHatchRecords.getEcologyHatchId());
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("熔炼配置不存在"));
        // 孵化周期(天)
        Integer hatchCycleDay = userEcologyHatchRecords.getHatchCycle();
        for (EcologyHatchRecordsRewards ecologyHatchRecordsReward : ecologyHatchRecordsRewards) {
            // 时间根据熔炼时间来
            Date recordTime = userEcologyHatchRecords.getCreateTime();
            // 奖励周期
            Integer rewardsCycle = ecologyHatchRecordsReward.getRewardsCycle();
            if (ecologyHatchRecordsReward.getExtraReward()) {
                if (rewardsCycle > hatchCycleDay) {
                    continue;
                }
                UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecordId);
                userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRecordsReward.getRewardsType());
                userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRecordsReward.getLinkId());
                userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRecordsReward.getLinkName());
                userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRecordsReward.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRecordsReward.getLinkImage());
                userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                userPredictEcologyHatchRewardsRecords.setExtraReward(ecologyHatchRecordsReward.getExtraReward());
                userPredictEcologyHatchRewardsRecords.setSettlementTime(DateUtil.offsetDay(userEcologyHatchRecords.getCreateTime(), rewardsCycle));
                userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
            } else {
                // 奖励次数
                Integer rewardsCount = BigDecimalUtils.divide(hatchCycleDay, rewardsCycle, 0, RoundingMode.DOWN).intValue();
                for (int i = 0; i < rewardsCount; i++) {
                    recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                    UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                    userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                    userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecordId);
                    userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                    userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRecordsReward.getRewardsType());
                    userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRecordsReward.getLinkId());
                    userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRecordsReward.getLinkName());
                    userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRecordsReward.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                    userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRecordsReward.getLinkImage());
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                    userPredictEcologyHatchRewardsRecords.setSettlementTime(recordTime);
                    userPredictEcologyHatchRewardsRecords.setExtraReward(ecologyHatchRecordsReward.getExtraReward());
                    userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
                }
            }
        }
        userPredictEcologyHatchRewardsRecordsDao.saveList(userPredictEcologyHatchRewardsRecordsList);
    }

    /**
     * 延长周期
     * @param userEcologyHatchRecords
     * @param now
     * @return
     */
    private List<UserPredictEcologyHatchRewardsRecords> scriptOther(UserEcologyHatchRecords userEcologyHatchRecords, Date now) {
        List<UserPredictEcologyHatchRewardsRecords> userPredictEcologyHatchRewardsRecordsList = new ArrayList<>();
        Boolean hasList = userPredictEcologyHatchRewardsRecordsDao.hasList(userEcologyHatchRecords.getUserId(), userEcologyHatchRecords.getId());
        if (hasList){
            return userPredictEcologyHatchRewardsRecordsList;
        }
        // 共创id集合
        List<Long> createTogetherIds = Lists.newArrayList(358L,
                359L,
                360L,
                361L,
                362L,
                363L,
                364L,
                365L,
                366L,
                367L,
                368L,
                369L,
                370L,
                371L,
                372L,
                373L,
                374L,
                375L,
                376L,
                405L,
                411L,
                417L,
                437L,
                449L,
                460L,
                474L,
                486L,
                519L,
                524L);
        // 获取快照奖励
        List<EcologyHatchRecordsRewards> ecologyHatchRecordsRewards = ecologyHatchRecordsRewardsDao.getListByEcologyHatchRecordsId(userEcologyHatchRecords.getId());
        Assert.isFalse(CollectionUtils.isEmpty(ecologyHatchRecordsRewards), () -> new GlobalRunTimeException("快照奖励不存在"));
        // 获取熔炼配置
        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(userEcologyHatchRecords.getEcologyHatchId());
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("熔炼配置不存在"));
        // 获取熔炼奖励（这个是修改之后的）
        List<EcologyHatchRewardsConf> rewardsConfList = ecologyHatchRewardsConfDao.getListByEcologyHatchId(userEcologyHatchRecords.getEcologyHatchId(), userEcologyHatchRecords.getMethodId());
        // 获取熔炼配置的铸造配置以及藏品配置
        EcologyCastConf ecologyCastConf = ecologyCastConfDao.getOneByCollectionId(ecologyHatchConf.getCollectionId());
        // 获取铸造配置消耗的道具
        EcologyCastExpendConf ecologyCastExpendConf = ecologyCastExpendConfDao.getListByCastId(ecologyCastConf.getId());
        // 获取奖励的道具
        EcologyHatchRecordsRewards recordsRewards = ecologyHatchRecordsRewards.stream().filter(hatchRecordsRewards -> hatchRecordsRewards.getRewardsType().equals(EcologyRewardsType.EQUITY_PROPS) && !createTogetherIds.contains(hatchRecordsRewards.getLinkId())).findFirst().get();
        // 获取周期截断时间
        Date periodTruncationTime = DateUtil.beginOfDay(userEcologyHatchRecords.getCreateTime());
        periodTruncationTime = DateUtil.offsetDay(periodTruncationTime, -1);
        Integer otherRewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), recordsRewards.getRewardsCycle(), 0, RoundingMode.DOWN).intValue();
        for (int i = 1; i <= otherRewardsCount; i++) {
            periodTruncationTime = DateUtil.offsetDay(periodTruncationTime, recordsRewards.getRewardsCycle());
            int count = i * recordsRewards.getLinkCount().intValue();
            int expendCount = ecologyCastExpendConf.getExpendCount().intValue();
            // 再加一个周期
            if (periodTruncationTime.after(DateUtil.offsetDay(now, recordsRewards.getRewardsCycle())) && count % expendCount == 0) {
                // 超过当前时间并且能整除
                break;
            }
        }
        for (EcologyHatchRecordsRewards ecologyHatchRecordsReward : ecologyHatchRecordsRewards) {
            // 时间根据熔炼时间来
            Date recordTime = userEcologyHatchRecords.getCreateTime();
            recordTime = DateUtil.offsetDay(recordTime, -1);
            // 需要补扣
            Boolean need = Boolean.TRUE;
            // 奖励周期
            Integer rewardsCycle = ecologyHatchRecordsReward.getRewardsCycle();
            // 奖励次数
            Integer rewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), rewardsCycle, 0, RoundingMode.DOWN).intValue();
            for (int i = 0; i < rewardsCount; i++) {
                if (recordTime.after(periodTruncationTime)) {
                    continue;
                }
                recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecords.getId());
                userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRecordsReward.getRewardsType());
                userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRecordsReward.getLinkId());
                userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRecordsReward.getLinkName());
                userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRecordsReward.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRecordsReward.getLinkImage());
                if (recordTime.before(now)) {
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.TRUE);
                }else {
                    if (DateUtil.offsetDay(recordTime, rewardsCycle).after(periodTruncationTime) && EcologyRewardsType.EQUITY_PROPS.equals(ecologyHatchRecordsReward.getRewardsType())  && need && !createTogetherIds.contains(ecologyHatchRecordsReward.getLinkId())){
                        // 最后一个周期
                        userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.TRUE);
                        need = Boolean.FALSE;
                    }else {
                        userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                    }
                }
                userPredictEcologyHatchRewardsRecords.setSettlementTime(recordTime);
                userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
            }
        }
        for (EcologyHatchRewardsConf ecologyHatchRewardsConf : rewardsConfList) {
            // 时间根据熔炼时间来
            Date recordTime = userEcologyHatchRecords.getCreateTime();
            recordTime = DateUtil.offsetDay(recordTime, -1);
            // 奖励周期
            Integer rewardsCycle = ecologyHatchRewardsConf.getRewardsCycle();
            // 奖励次数
            Integer rewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), rewardsCycle, 0, RoundingMode.DOWN).intValue();
            for (int i = 0; i < rewardsCount; i++) {
                if (recordTime.after(periodTruncationTime)) {
                    recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                    UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                    userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                    userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecords.getId());
                    userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                    userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRewardsConf.getRewardsType());
                    userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRewardsConf.getLinkId());
                    userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRewardsConf.getLinkName());
                    userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRewardsConf.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                    userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRewardsConf.getLinkImage());
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                    userPredictEcologyHatchRewardsRecords.setSettlementTime(recordTime);
                    userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
                }else {
                    recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                }
            }

        }
        return userPredictEcologyHatchRewardsRecordsList;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void script() {
        List<UserPredictEcologyHatchRewardsRecords> scriptList = new ArrayList<>();
        List<UserEcologyHatchRecords> settleList = userEcologyHatchRecordsDao.otherScriptList();
        Date now = DateUtil.endOfDay(new Date());
        for (UserEcologyHatchRecords userEcologyHatchRecords : settleList) {
            List<UserPredictEcologyHatchRewardsRecords> script = scriptOther(userEcologyHatchRecords,now);
            scriptList.addAll(script);
        }
        List<UserEcologyHatchRecords> records = userEcologyHatchRecordsDao.scriptList();
        for (UserEcologyHatchRecords userEcologyHatchRecords : records) {
            List<UserPredictEcologyHatchRewardsRecords> script2 = scriptAll(userEcologyHatchRecords,now);
            scriptList.addAll(script2);
        }
        if (!CollectionUtils.isEmpty(scriptList)) {
            List<List<UserPredictEcologyHatchRewardsRecords>> list = CollUtil.split(scriptList, 10000);
            for (List<UserPredictEcologyHatchRewardsRecords> split : list) {
                userPredictEcologyHatchRewardsRecordsDao.saveList(split);
            }
        }
    }

    /**
     * 偏移周期
     * @param userEcologyHatchRecords
     * @param now
     * @return
     */
    private List<UserPredictEcologyHatchRewardsRecords> scriptAll(UserEcologyHatchRecords userEcologyHatchRecords, Date now) {
        List<UserPredictEcologyHatchRewardsRecords> userPredictEcologyHatchRewardsRecordsList = new ArrayList<>();
        Boolean hasList = userPredictEcologyHatchRewardsRecordsDao.hasList(userEcologyHatchRecords.getUserId(), userEcologyHatchRecords.getId());
        if (hasList){
            return userPredictEcologyHatchRewardsRecordsList;
        }
        // 共创id集合
        List<Long> createTogetherIds = Lists.newArrayList(358L,
                359L,
                360L,
                361L,
                362L,
                363L,
                364L,
                365L,
                366L,
                367L,
                368L,
                369L,
                370L,
                371L,
                372L,
                373L,
                374L,
                375L,
                376L,
                405L,
                411L,
                417L,
                437L,
                449L,
                460L,
                474L,
                486L,
                519L,
                524L);
        // 获取快照奖励
        List<EcologyHatchRecordsRewards> ecologyHatchRecordsRewards = ecologyHatchRecordsRewardsDao.getListByEcologyHatchRecordsId(userEcologyHatchRecords.getId());
        Assert.isFalse(CollectionUtils.isEmpty(ecologyHatchRecordsRewards), () -> new GlobalRunTimeException("快照奖励不存在"));
        // 获取熔炼配置
        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(userEcologyHatchRecords.getEcologyHatchId());
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("熔炼配置不存在"));
        // 获取熔炼奖励（这个是修改之后的）
        List<EcologyHatchRewardsConf> rewardsConfList = ecologyHatchRewardsConfDao.getListByEcologyHatchId(userEcologyHatchRecords.getEcologyHatchId(), userEcologyHatchRecords.getMethodId());
        // 获取熔炼配置的铸造配置以及藏品配置
        EcologyCastConf ecologyCastConf = ecologyCastConfDao.getOneByCollectionId(ecologyHatchConf.getCollectionId());
        // 获取铸造配置消耗的道具
        EcologyCastExpendConf ecologyCastExpendConf = ecologyCastExpendConfDao.getListByCastId(ecologyCastConf.getId());
        // 获取奖励的道具
        EcologyHatchRecordsRewards recordsRewards = ecologyHatchRecordsRewards.stream().filter(hatchRecordsRewards -> hatchRecordsRewards.getRewardsType().equals(EcologyRewardsType.EQUITY_PROPS) && !createTogetherIds.contains(hatchRecordsRewards.getLinkId())).findFirst().get();
        // 获取周期截断时间
        Date periodTruncationTime = DateUtil.beginOfDay(userEcologyHatchRecords.getCreateTime());
        periodTruncationTime = DateUtil.offsetDay(periodTruncationTime, -1);
        Integer otherRewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), recordsRewards.getRewardsCycle(), 0, RoundingMode.DOWN).intValue();
        for (int i = 1; i <= otherRewardsCount; i++) {
            periodTruncationTime = DateUtil.offsetDay(periodTruncationTime, recordsRewards.getRewardsCycle());
            int count = i * recordsRewards.getLinkCount().intValue();
            int expendCount = ecologyCastExpendConf.getExpendCount().intValue();
            if (periodTruncationTime.after(now) && count % expendCount == 0) {
                // 超过当前时间并且能整除
                break;
            }
        }
        for (EcologyHatchRecordsRewards ecologyHatchRecordsReward : ecologyHatchRecordsRewards) {
            // 时间根据熔炼时间来
            Date recordTime = userEcologyHatchRecords.getCreateTime();
            recordTime = DateUtil.offsetDay(recordTime, -1);

            // 奖励周期
            Integer rewardsCycle = ecologyHatchRecordsReward.getRewardsCycle();
            // 奖励次数
            Integer rewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), rewardsCycle, 0, RoundingMode.DOWN).intValue();
            for (int i = 0; i < rewardsCount; i++) {
                if (recordTime.after(periodTruncationTime)) {
                    continue;
                }
                recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecords.getId());
                userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRecordsReward.getRewardsType());
                userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRecordsReward.getLinkId());
                userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRecordsReward.getLinkName());
                userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRecordsReward.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRecordsReward.getLinkImage());
                if (recordTime.before(now)) {
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.TRUE);
                    if (recordTime.after(DateUtil.beginOfDay(now)) && EcologyRewardsType.EQUITY_PROPS.equals(ecologyHatchRecordsReward.getRewardsType())){
                        userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                    }
                }else {
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                }
                userPredictEcologyHatchRewardsRecords.setSettlementTime(recordTime);
                userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
            }
        }
        for (EcologyHatchRewardsConf ecologyHatchRewardsConf : rewardsConfList) {
            // 时间根据熔炼时间来
            Date recordTime = userEcologyHatchRecords.getCreateTime();
            recordTime = DateUtil.offsetDay(recordTime, -1);
            // 奖励周期
            Integer rewardsCycle = ecologyHatchRewardsConf.getRewardsCycle();
            // 奖励次数
            Integer rewardsCount = BigDecimalUtils.divide(userEcologyHatchRecords.getHatchCycle(), rewardsCycle, 0, RoundingMode.DOWN).intValue();
            for (int i = 0; i < rewardsCount; i++) {
                if (recordTime.after(periodTruncationTime)) {
                    recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                    UserPredictEcologyHatchRewardsRecords userPredictEcologyHatchRewardsRecords = new UserPredictEcologyHatchRewardsRecords();
                    userPredictEcologyHatchRewardsRecords.setUserId(userEcologyHatchRecords.getUserId());
                    userPredictEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(userEcologyHatchRecords.getId());
                    userPredictEcologyHatchRewardsRecords.setEcologyHatchId(ecologyHatchConf.getId());
                    userPredictEcologyHatchRewardsRecords.setRewardsType(ecologyHatchRewardsConf.getRewardsType());
                    userPredictEcologyHatchRewardsRecords.setLinkId(ecologyHatchRewardsConf.getLinkId());
                    userPredictEcologyHatchRewardsRecords.setLinkName(ecologyHatchRewardsConf.getLinkName());
                    userPredictEcologyHatchRewardsRecords.setLinkCount(ecologyHatchRewardsConf.getLinkCount().multiply(Convert.toBigDecimal(userEcologyHatchRecords.getHatchCount())));
                    userPredictEcologyHatchRewardsRecords.setLinkImage(ecologyHatchRewardsConf.getLinkImage());
                    userPredictEcologyHatchRewardsRecords.setIsSettle(Boolean.FALSE);
                    userPredictEcologyHatchRewardsRecords.setSettlementTime(recordTime);
                    userPredictEcologyHatchRewardsRecordsList.add(userPredictEcologyHatchRewardsRecords);
                }else {
                    recordTime = DateUtil.offsetDay(recordTime, rewardsCycle);
                }
            }

        }
        return userPredictEcologyHatchRewardsRecordsList;

    }
}
